home *** CD-ROM | disk | FTP | other *** search
- /*
- File: Document.cp
-
- Contains: An abstract document class
-
- Written by: Dave Falkenburg
-
- Copyright: © 1994-95 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
- <1> 1/24/95 DRF First checked in.
- To Do: Integrate Marshall’s printing loop
- Mix-in Eric’s MAEObject to make things scriptable
- */
-
- #include "Document.h"
-
- #include "Sprocket.h"
- #include <TextUtils.h>
-
-
- // internal function prototypes and UPPs:
-
- static pascal Boolean CloseDialogFilterProc(DialogPtr theDialog, EventRecord* anEvent, short* itemHit);
- static ModalFilterUPP CloseDialogFilterUPP = NewModalFilterProc(CloseDialogFilterProc);
-
-
- TDynamicArray TDocument::fgActiveDocuments;
-
-
- TDocument::TDocument(LetterDescriptor * theContainer /* = NULL */)
- {
- fgActiveDocuments.InsertFirst(this);
-
- if (theContainer)
- {
- fNeedsSave = false;
- fHasBeenSaved = true;
- fHasNewEditions = false;
- }
- else
- {
- fNeedsSave = true;
- fHasBeenSaved = false;
- fHasNewEditions = false;
- }
- }
-
-
- TDocument::~TDocument()
- {
- fgActiveDocuments.Delete(this);
- }
-
-
- Boolean
- TDocument::DoMenuCommand(MenuCommandID command)
- {
- switch (command)
- {
- case cClose:
- this->Close(false);
- break;
-
- case cSave:
- if (fHasBeenSaved)
- this->Save();
- else
- this->SaveAs();
- break;
-
- case cSaveAs:
- this->SaveAs();
- break;
-
- case cPageSetup:
- case cCustomPageSetup:
- this->PageSetup(command == cCustomPageSetup);
- break;
-
- case cPrint:
- case cPrintOne:
- this->Print(command == cPrint);
- break;
-
- default:
- return false;
- }
-
- return true;
- }
-
-
- StringPtr
- TDocument::GetDocumentName(void)
- {
- return "\pUntitled";
- }
-
-
- Boolean
- TDocument::Close(Boolean quitting)
- {
- OSErr err = noErr;
-
- if (fNeedsSave)
- {
- CloseResult whatShouldWeDo = this->CloseDialog(quitting);
-
- if (whatShouldWeDo == kCancelCloseDocument)
- return false;
-
- else if (whatShouldWeDo != kDontSaveDocument)
- {
- if (fHasBeenSaved)
- err = this->Save();
- else
- err = this->SaveAs();
- }
- }
-
- if (err == noErr) // Only allow to close if we successfully saved!
- {
- delete this; // Get rid of us, and our little windows, too!
- return true;
- }
-
- return false;
- }
-
-
- //////////////////////////////////////////////////////////////////
- //
- // CloseDialog
- //
- // Provides the standard human interface for closing a document
-
- TDocument::CloseResult
- TDocument::CloseDialog(Boolean quitting)
- {
- StringPtr nullStr = (StringPtr) "\p";
- Str255 reasonForClosingStr;
- short whichAlert;
- short whichString;
-
- if (fHasNewEditions)
- whichAlert = kStandardCloseWithNewPubsAlertID;
- else
- whichAlert = kStandardCloseAlertID;
-
- if (quitting)
- whichString = kQuittingStr;
- else
- whichString = kClosingStr;
-
- GetIndString(reasonForClosingStr,kStandardCloseStrings,whichString);
- ParamText(GetDocumentKind(),GetDocumentName(),reasonForClosingStr,nullStr);
-
- return ((CloseResult) StandardAlert(whichAlert,kSaveDocument,kCancelCloseDocument,CloseDialogFilterUPP));
- }
-
-
- static pascal Boolean
- CloseDialogFilterProc(DialogPtr theDialog, EventRecord* anEvent, short* itemHit)
- {
- if ((anEvent->what == keyDown))
- {
- char c = (char) anEvent->message & charCodeMask;
-
- if ((c == 'd') || (c == 'D')) // NOT INTERNATIONAL FRIENDLY!!!
- {
- *itemHit = TDocument::kDontSaveDocument;
- PseudoClickInDialogItem(theDialog,*itemHit);
- return true;
- }
- }
-
- // Return through the common code above so that default item processing can happen
- return StandardDialogFilterProc(theDialog, anEvent, itemHit);
- }
-
-
- /* static */ Boolean
- TDocument::CloseAllDocuments()
- {
- TDocument *aDocument;
-
- while ((aDocument = (TDocument *) fgActiveDocuments.First()) != NULL)
- {
- if (aDocument->Close(true) == false)
- break;
- }
-
-
- // If we got through all the documents, return true
- return (aDocument == NULL);
- }
-
-
- void
- TDocument::BringDocumentToFront()
- {
- fgActiveDocuments.MoveToFront((ArrayElementPtr) this);
- }
-
-
- OSErr
- TDocument::Save()
- {
- return -1;
- }
-
-
- OSErr
- TDocument::SaveAs()
- {
- return -1;
- }
-
-
- OSErr
- TDocument::Revert()
- {
- return -1;
- }
-
-
- // printing methods
-
- void
- TDocument::PageSetup(Boolean /* useCustomPageSetup */)
- {
- }
-
-
- void
- TDocument::Print(Boolean /* usePrintJobDialog */)
- {
- }
-